package com.qfedu.dao.impl;

import com.qfedu.dao.OrderDao;
import com.qfedu.entity.*;
import com.qfedu.utils.BaseDao;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import javax.persistence.criteria.Order;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class OrderDaoImpl extends BaseDao implements OrderDao {
    @Override
    public List<Cart> selectCart(int uid) throws SQLException, InvocationTargetException, IllegalAccessException {
        String sql = "select p.p_name as pname,p.p_id as pid,p.t_id as tid," +
                "p.p_time as ptime,p.p_image as pimage,p_state as pstate," +
                "p.p_info as pinfo, p.p_price as pprice,c.c_id as cid,c.u_id as uid,c.c_count as ccount," +
                "c.c_num as cnum from product p join cart c on p.p_id = c.p_id where" +
                " c.u_id = ?;";
        List<Cart> cartLsit = new ArrayList<>();
        List<Map<String, Object>> list = queryRunner.query(sql, new MapListHandler(), uid);
        for (Map<String, Object> map : list) {
            // 将map 中的数据 封装到cart中
            Cart cart = new Cart();
            Product product = new Product();
            BeanUtils.populate(cart, map);
            BeanUtils.populate(product, map);
            cart.setProduct(product);
            cartLsit.add(cart);
        }
        return cartLsit;
    }
    @Override
    public List<Address> selectAddress(int uid) throws SQLException {
            String sql = "select a_id as aid, u_id as uid,a_name as aname," +
                    "a_phone as aphone,a_detail as adetail ,a_state as astate " +
                    "from address where u_id = ?";
            List<Address> addressList = queryBeanList(sql, Address.class, uid);
            return addressList;
    }

    @Override
    public List<Orders> getOrderListByUid(int uid) throws SQLException, InvocationTargetException, IllegalAccessException {
        String sql = "select o.o_id as oid,o.u_id as uid,o.a_id as aid," +
                "o.o_count as ocount,o.o_time as otime,o.o_state as ostate," +
                "a.a_name as aname,a.a_phone as aphone,a.a_detail as adetail, " +
                "a.a_state as astate from address a join orders o on a.a_id = o.a_id " +
                "where o.u_id = ?;";
        List<Orders> ordersList = new ArrayList<>();
        List<Map<String, Object>> list = queryRunner.query(sql, new MapListHandler(), uid);
        for (Map<String, Object> map : list) {
            // 将map 中的数据 封装到cart中
            Orders orders = new Orders();
            Address address = new Address();
            BeanUtils.populate(orders, map);
            BeanUtils.populate(address, map);
            orders.setAddress(address);
            ordersList.add(orders);
        }
        return ordersList;
    }

    @Override
    public int addOrder(String oid, int uid, int aid, BigDecimal ocount,String otime,int ostate) throws SQLException {
        String sql = "insert into orders (o_id,u_id,a_id,o_count,o_time,o_state) values(?,?,?,?,?,?)";
        int num = update(sql, oid,uid,aid,ocount,otime,ostate);
        return num;
    }

    @Override
    public int addItem(String oid, int pid, BigDecimal icount, int inum) throws SQLException {
        String sql = "insert into item(o_id,p_id,i_count,i_num) value(?,?,?,?)";
        int update = update(sql, oid, pid, icount, inum);
        return update;
    }

    @Override
    public Orders queryOrderAddress(String oid) throws SQLException, InvocationTargetException, IllegalAccessException {
        String sql = "select o.o_id as oid,o.u_id as uid,o.a_id as aid," +
                "o.o_count as ocount,o.o_time as otime,o.o_state as ostate," +
                "a.a_name as aname,a.a_phone as aphone,a.a_detail as adetail, " +
                "a.a_state as astate from address a join orders o on a.a_id = o.a_id " +
                "where o.o_id = ?";
        Map<String, Object> query = queryRunner.query(sql, new MapHandler(),oid);
        if (query == null) {
            return null;
        }

        Orders orders = new Orders();
        Address address = new Address();

        BeanUtils.populate(orders,query);
        BeanUtils.populate(address, query);

        orders.setAddress(address);

        return orders;
    }

    @Override
    public List<Item> queryItemInfo(String oid) throws SQLException, InvocationTargetException, IllegalAccessException {
        String sql = "select p.p_id as pid, p.t_id as tid,p.p_name as pname,p.p_time as ptime ," +
                "p.p_image as pimage,p.p_state as pstate ,p.p_info as pinfo ,p.p_price as pprice," +
                "i.o_id as oid, i.i_id as iid ,i.i_count as icount,i.i_num as inum " +
                "from product p join item i on p.p_id = i.p_id  where i.o_id = ?";
        List<Item> itemList = new ArrayList<>();
        List<Map<String, Object>> list = queryRunner.query(sql, new MapListHandler(),oid);
        for (Map<String, Object> map : list) {
            // 将map 中的数据 封装到cart中
            Item item = new Item();
            Product product = new Product();
            BeanUtils.populate(item, map);
            BeanUtils.populate(product, map);
            item.setProduct(product);
            itemList.add(item);
        }
        return itemList;
    }

    @Override
    public List<Cart> getCartListByUid(int uid) throws SQLException, InvocationTargetException, IllegalAccessException {
        String sql = "select p.p_name as pname,p.p_id as pid,p.t_id as tid," +
                "p.p_time as ptime,p.p_image as pimage,p_state as pstate," +
                "p.p_info as pinfo, p.p_price as pprice,c.c_id as cid,c.u_id as uid,c.c_count as ccount," +
                "c.c_num as cnum from product p join cart c on p.p_id = c.p_id where" +
                " c.u_id = ?;";
        List<Cart> cartLsit = new ArrayList<>();
        List<Map<String,Object>> list = queryRunner.query(sql,new MapListHandler(),uid);
        for(Map<String,Object> map:list){
            // 将map 中的数据 封装到cart中
            Cart cart = new Cart();
            Product product = new Product();
            BeanUtils.populate(cart,map);
            BeanUtils.populate(product, map);
            cart.setProduct(product);
            cartLsit.add(cart);
        }
        return cartLsit;
    }

    @Override
    public int deleteCart(int uid) throws SQLException {
        String sql = "delete from cart where u_id = ?";
        int num = update(sql, uid);
        return num;
    }

    @Override
    public int updateState(int ostate, String oid) throws SQLException {
        String sql = "update orders set o_state = ? where o_id = ?";
        int num = update(sql,ostate,oid);
        return num;
    }
}

